/*
 * CommandProcessor.cpp
 *
 */

#include "CommandProcessor.h"
#include <iostream>
#include "../utils/Utils.h"
#include "../persistence/secuential/SecuentialSort.h"
#include "../StringRecord.h"
#include "../StringRecordReader.h"

CommandProcessor::CommandProcessor() {
}

CommandProcessor::~CommandProcessor() {
}

void CommandProcessor::massiveLoad(const String& name, const String& file) {
	StringRecordReader reader;
	if (!reader.open(file)) {
		std::cout << "archivo no encontrado" << std::endl;
		return;
	}

	SecuentialFile<StringRecord> block(BLOCK_SIZE, 1);
	block.open(name, true);

	StringRecord record;
	while (reader.next(record)) {
		block.add(record);
	}
}

void CommandProcessor::listFile(const String& path) {
	SecuentialFile<StringRecord> block(BLOCK_SIZE, 1);
	if (!block.open(path)) {
		std::cout << "archivo no encontrado" << std::endl;
		return;
	}

	block.rewind();
	StringRecord result;
	int x = 0;
	while (block.next(result)) {
		x++;
		std::cout << result << std::endl;
	}
	std::cout << "total registros: " << x << std::endl;
}

void CommandProcessor::list(const String& name) {
	listFile(name);
}

void CommandProcessor::remove(const String& name, const String& id) {
//	SecuentialFile<StringRecord> block(BLOCK_SIZE);
//	block.open(name + ENTITY_EXT);
//
//	StringRecord result;
//	if (block.remove(result, StringRecord::byId(Utils::getInt(id)))) {
//		std::cout << "se removio " << result << std::endl;
//	}
}

void CommandProcessor::individualLoad(const String& name, const String& id,
														  const String& value) {
//	SecuentialFile<StringRecord> blockFile(BLOCK_SIZE);
//	if (!blockFile.open(name + ENTITY_EXT)) {
//		std::cout << "archivo no encontrado" << std::endl;
//		return;
//	}
//
//	StringRecord record(Utils::getInt(id), value);
//	blockFile.load(record);
}

void CommandProcessor::find(const String& name, const IEvaluator<StringRecord>& eval) {
	SecuentialFile<StringRecord> block(BLOCK_SIZE, 1);
	if (!block.open(name)) {
		std::cout << "archivo no encontrado" << std::endl;
		return;
	}

	StringRecord result;
	if (block.find(result, eval)) {
		std::cout << "se encontro " << result << std::endl;
	}
}

void CommandProcessor::findById(const String& name, const String& id) {
	find(name, StringRecord::byId(Utils::parseInt(id)));
}

void CommandProcessor::findByValue(const String& name, const String& value) {
	find(name, StringRecord::byValue(value));
}

void CommandProcessor::sort(const String& name, Compare compare) {
	SecuentialFile<StringRecord> block(BLOCK_SIZE);
	if (!block.open(name + ENTITY_EXT)) {
		std::cout << "archivo no encontrado" << std::endl;
		return;
	}

	SecuentialSort<StringRecord> sort(INNERSORT_SIZE, BLOCK_SIZE, MERGE_LINES);
	sort.setTempName(name);
	sort.setMergeName(name);
	SecuentialFile<StringRecord> result(BLOCK_SIZE);

	String path = sort.sort(block, compare);
	if (path.length() > 0)
		listFile(path);
	else
		std::cout << "no se pudo ordenar el archivo" << std::endl;
}

void CommandProcessor::sortById(const String& name) {
	sort(name, StringRecord::compByIdDesc);
}

void CommandProcessor::sortByValue(const String& name) {
	sort(name, StringRecord::compByValDesc);
}
